package net.expectx.pay.rpc.service.impl;

import net.expectx.common.single.base.BaseConstants;
import net.expectx.common.single.base.BaseResult;
import net.expectx.common.single.base.BaseServiceImpl;
import net.expectx.pay.dao.bean.ProductOrderHeader;
import net.expectx.pay.dao.mapper.ProductOrderHeaderMapper;
import net.expectx.pay.dao.mapper.ProductOrderPaymentRecordMapper;
import net.expectx.pay.dao.bean.ProductOrderPaymentRecord;
import net.expectx.pay.rpc.api.ProductOrderPaymentRecordService;
import net.expectx.pay.dao.bean.ProductOrderPaymentRecordExample;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * ProductOrderPaymentRecordService实现
 * @author lijian
 * @date 2019/7/4
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProductOrderPaymentRecordServiceImpl extends BaseServiceImpl<ProductOrderPaymentRecord, ProductOrderPaymentRecordExample> implements ProductOrderPaymentRecordService {

    private static final Logger LOGGER = Logger.getLogger(ProductOrderPaymentRecordServiceImpl.class);

    @Autowired
    ProductOrderPaymentRecordMapper productOrderPaymentRecordMapper;
    @Autowired
    ProductOrderHeaderMapper productOrderHeaderMapper;
    @Autowired
    private DataSourceTransactionManager transactionManager;
    @Override
    public BaseResult doPayResult(ProductOrderHeader orderHeader, ProductOrderPaymentRecord orderPaymentRecord) {
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        TransactionStatus status = transactionManager.getTransaction(def);
        try {
            productOrderHeaderMapper.updateByPrimaryKeySelective(orderHeader);
            productOrderPaymentRecordMapper.updateByPrimaryKeySelective(orderPaymentRecord);
            transactionManager.commit(status);
        }catch (Exception e){
            e.printStackTrace();
            transactionManager.rollback(status);
        }
        return null;
    }
}
